class GameWindow extends CanvasWindow { 
  
  PlayerShip playerShip;
  MovingBackground BG;
  CollisionManager colManager;
  EnemySpawner spawner;
  int lastTime;
  SoundManager soundManager;
  ImageElement img;
  
  AudioContext a;
  GameWindow(Element element):super(element)
  { 
    colManager = new CollisionManager();
    
    BG = new MovingBackground(width, height);
    addChild(BG);
    
    playerShip = new PlayerShip();
    addChild(playerShip);
    colManager.addObject(playerShip);
    
    spawner = new EnemySpawner(this);
    
    this.setX(this.element.$dom_getBoundingClientRect().left.toInt());
    this.setY(this.element.$dom_getBoundingClientRect().top.toInt());
    
    lastTime = new Date.now().milliseconds;   
    window.webkitRequestAnimationFrame(preLoadAssets);
    
    AssetsLoader.loadAll();
  }
  
  bool preLoadAssets(int time)
  {
    if(AssetsLoader.finishedLoading)
    {
      onEnterFrame(time);
    }
    else
    {
      window.webkitRequestAnimationFrame(preLoadAssets);
    }
  }
  bool onEnterFrame(int time)
  {
    update(time-lastTime);
    draw();
    lastTime = time;
    window.webkitRequestAnimationFrame(onEnterFrame);
  }
  
  void update(int time)
  {
    //if(time > 20) print("$time");
    for(ElementWindow child in children)
    {
      child.update(time);
    }
    
    colManager.checkCollisions();
  }
}

void main() {
  
  new GameWindow(document.query("#GameCanvas"));
}